Repository navigation
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7a5245b2dd
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
|
||
| batch_ptrs, batch_sizes, _ = self._preprocess_tensors_for_put(batch_tensors) | ||
| batch_ptrs, batch_sizes, batch_tensors = self._preprocess_tensors_for_put(batch_tensors) | ||
| if self._buffer_pool is not None and len(set(batch_keys)) == len(batch_keys): |
There was a problem hiding this comment.
Keep NPU tensors on the direct registration path
When protocol="rdma" is used with an Ascend/NPU TensorDict, _preprocess_tensors_for_put preserves NPU tensors (it only calls .cpu() for CUDA), yet this unconditional pool branch sends them to _copy_between_buffer_and_tensors, whose torch.frombuffer destination is CPU memory. The previous path registered each NPU data_ptr() directly (and _bind_npu_device exists to make those registrations work); now every pool-sized NPU value pays a synchronous device-to-host copy and loses the direct-device transfer path, while only oversized or pool-exhausted groups retain the old behavior. Restrict pooling to CPU-backed tensors so NPU puts keep the registered-pointer path.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
@Chase-Rong Please help to verify whether this comment is true?
There was a problem hiding this comment.
@Chase-Rong Please help to verify whether this comment is true?
Thanks for flagging this. I'm not very familiar with the NPU path and don't have NPU hardware available to verify this behavior. If confirmed, we can restrict pooling to CPU tensors and let NPU tensors fall back to the existing register/unregister path.
There was a problem hiding this comment.
@Chase-Rong Please help to verify whether this comment is true?
OK, I can verify this
0oshowero0
left a comment
There was a problem hiding this comment.
[AI Review] Pooled PUT send-buffer lifetime
| except Exception as release_error: | ||
| logger.warning(f"Returning the pooled send buffer failed: {release_error}") | ||
| raise | ||
| lease.release() |
There was a problem hiding this comment.
[AI Review] Keep the send buffer leased until RDMA has finished using it
In Mooncake 0.3.12.post1, used for the benchmark, a transfer can still be pending when the 60-second wait returns. The classic freeBatchID() returns BatchBusy for unfinished tasks, and the destructor discards that result. When this code releases the lease, the next PUT group can immediately overwrite the same registered source address. If a queued write from the old group is posted later, it can send the new group's bytes to the old destination. Timeout path · Batch cleanup
Mooncake #4491 reproduced a late write overwriting a reused destination under RDMA fault injection. I verified the immediate source address reuse with a fake store. The combined scenario still needs hardware verification. Mooncake #4491
Before enabling pooled PUT by default, please establish that batch_upsert_from() leaves the source buffer safe to reuse after every attempt, including an attempt that times out before a retry succeeds. Mooncake can also retain the lease until all attempts have stopped accessing the source.
There was a problem hiding this comment.
Thanks for the review.
Mooncake < 0.3.13 (incl. 0.3.12.post1 used in the benchmark): a native wait timeout is returned to TQ as success, so TQ never retries. This doesn't depend on pooling and affects main too. Details in #218.
Mooncake >= 0.3.13: the timed-out key returns -800, and _batch_upsert_with_retry retries it from the same buffer. _write_via_buffer_pool keeps the lease across all attempts and releases it only after the final result, so the buffer is not reused while retries are running. The case above, where a later group overwrites the source, cannot happen during retry.
Given this, I suggest raising the Mooncake lower bound from >=0.3.10.post2 to >=0.3.13.
There was a problem hiding this comment.
[AI Review] Keep the send buffer leased until timed-out transfers have stopped
I agree that raising the minimum Mooncake version to 0.3.13 addresses #218. TQ also correctly keeps the same lease throughout the retry loop.
The remaining case is an initial attempt that times out, followed by a successful retry. TQ then releases the lease, and the next group in the same PUT can overwrite the source address. If the first attempt reads that address later, it can send the next group's bytes. Mooncake 0.3.14 defers cleanup of the native batch descriptor. TQ still releases its lease when the retry loop returns. Mooncake deferred cleanup
I confirmed immediate source-address reuse with a fake store after both a successful retry and retry exhaustion. Mooncake #4491 demonstrated a late write to a reused remote destination under RDMA fault injection. Corruption caused by local source-address reuse in #216 still needs hardware verification. Mooncake #4491
Before enabling pooled PUT by default, please ensure that the lease stays active until every attempt has stopped accessing the source buffer. If Mooncake can return earlier, the native operation needs to retain the lease until then.
There was a problem hiding this comment.
[AI Review] Keep the send buffer leased until timed-out transfers have stopped
I agree that raising the minimum Mooncake version to
0.3.13addresses #218. TQ also correctly keeps the same lease throughout the retry loop.The remaining case is an initial attempt that times out, followed by a successful retry. TQ then releases the lease, and the next group in the same PUT can overwrite the source address. If the first attempt reads that address later, it can send the next group's bytes. Mooncake
0.3.14defers cleanup of the native batch descriptor. TQ still releases its lease when the retry loop returns. Mooncake deferred cleanupI confirmed immediate source-address reuse with a fake store after both a successful retry and retry exhaustion. Mooncake #4491 demonstrated a late write to a reused remote destination under RDMA fault injection. Corruption caused by local source-address reuse in #216 still needs hardware verification. Mooncake #4491
Before enabling pooled PUT by default, please ensure that the lease stays active until every attempt has stopped accessing the source buffer. If Mooncake can return earlier, the native operation needs to retain the lease until then.
Agreed — since a timed-out transfer attempt may still read from the source buffer after returning, that buffer is not safe to reuse even when a subsequent retry succeeds.
To address this, I plan to quarantine any pooled send buffer that hits a transfer timeout (TRANSFER_FAIL, as Mooncake surfaces wait timeouts under this code), both when a later retry succeeds and when retries are exhausted: the client will keep holding that lease instead of returning it to the pool, and only release it during close(). Remaining and future transfers will keep using other available regions in the pool, and naturally fall back to direct buffer registration if the pool becomes exhausted. Even in that worst case, this introduces no performance regression — it simply reverts to the original unpooled behavior.
Does this approach look good to you? If so, I’ll update the PR accordingly.
There was a problem hiding this comment.
Thanks for the follow-up! Keeping a timed-out lease out of reuse does prevent subsequent pooled transfers from overwriting that registered source region at runtime, while Mooncake does not yet drain or retain buffers across a wait timeout (e.g., kvcache-ai/Mooncake#4780).
However, quarantining leases in the shared pool until close() has a few issues that break the "no regression / safe teardown" assumption:
-
PUT timeouts can permanently disable pooled GET ([optim] Lease pre-registered receive buffers on the MooncakeStore read path #175) and shrink read-staging headroom.
get()andput()share a singleBufferPool(..., max_regions=MAX_BATCH_WORKER_THREADS)(4regions) for the lifetime ofMooncakeStoreClient. Quarantined leases stay inin_use_andtotal_bytes_(buffer_pool.cpp#L513-L525). A single transient stall during one 4-workerput()(even if retries immediately succeed) can quarantine all 4 regions at once — permanently forcing bothput()andget()onto per-callregister_bufferfor the rest of the job (regressingmain's pooled GET), while pinning up to 50% oflocal_buffer_sizethat Mooncake's own read staging shares. -
Releasing quarantined leases in
close()is not safe if transfers are still in flight.
BufferPool.close()rejects active leases (buffer_pool.cpp#L358-L378) specifically becauseself._store.close()(tearDownAll) immediately unregisterslocal_bufferand freesclient_buffer_allocator_(real_client.cpp#L1982-L2031), whileMultiTransport::~MultiTransport()exits without waiting fordeferred_cleanup_batches_(multi_transport.cpp#L247-L256). Especially when retries exhaust and the caller immediately callsclose(), releasing quarantined leases first just bypasses that guard and tears down backing memory underneath pending transport work. -
Error tracking across retries has edge cases.
- In
_batch_upsert_with_retry, attempt 1 can time out with-800(TRANSFER_FAIL) while a later retry fails with another code (e.g.,-900/-200), so quarantine tracking would need to inspect every attempt (and unexpected exceptions), not only the final result. - Note that in Mooncake's
FinalizeBatchUpsert, aBatchUpsertRevokeresponse-size mismatch after a transfer timeout overwritesTRANSFER_FAIL (-800)withRPC_FAIL (-900)(client_service.cpp#L3566-L3575).
- In
Given these trade-offs, we'd prefer keeping pooled PUT opt-in behind a client config flag (default False) until Mooncake guarantees source-buffer safety on wait timeout upstream (such as kvcache-ai/Mooncake#4780), rather than maintaining stateful lease quarantine in TQ. If we still want a client-side quarantine fallback, it would at minimum need to: (a) cap PUT's pool usage / circuit-break pooled PUT on first timeout so quarantined PUT leases cannot starve pooled GET or local_buffer headroom, (b) avoid force-releasing quarantined leases prior to store.close(), and (c) log a warning and track -800 across all retry attempts. What do you think?
0oshowero0
left a comment
There was a problem hiding this comment.
A few additional notes on the current diff (7a5245b) when you update the PR:
- CPU-only guard for pooled PUT (
mooncake_client.py:387):_preprocess_tensors_for_putkeeps NPU tensors on device so worker threads (bound via_bind_npu_device, #179) canregister_bufferdevice pointers directly. Please gate the pooled PUT path ont.device.type == "cpu"so NPU tensors don't get copied into CPUtorch.frombufferviews. - Non-tensor path (
_put_bytes_thread_worker,mooncake_client.py:436-455):allocate_empty_tensorsalready packs the encoded batch into a single contiguous buffer (oneregister_buffercall). Routing variable-length serialized objects through_put_tensors_thread_workermisses theuniformfast path and adds per-itemfrombuffer+copy_overhead while competing for the 4 pool regions (similar to the fragmented small-key regression noted in #175). Could we either keep_put_bytes_thread_workeron the single-region registration path or share microbenchmark numbers for small/non-tensor batches? - Rebase & version warning (
mooncake_client.py:245):mainnow requiresmooncake-transfer-engine >= 0.3.13(#219) and updated license headers (#227); please rebase ontomainand update the fallback warning from>= 0.3.12to>= 0.3.13.
overview
Extend the pre-registered buffer pool approach from #175, which optimized GET, to CPU RDMA PUT.
Implementation
BufferPool, backed by the local buffer registered during store setup.Evaluation
Setup
Benchmarks ran on a single node with the PUT client and storage segment colocated, using RDMA with either 1 or 8 HCAs. Each HCA port reported an active Ethernet link rate of 200 Gb/s.
0.3.12.post1uint8tensorsLatency measures
await client.async_put(metadata=None), including metadata, storage, and controller work. Payload preparation, initialization, subsequent GET verification, and partition clearing run outside the timed region. The figure below presents the complete benchmark results.PUT latency
The table below includes the single-key, 1 GiB total, and largest tested cases for each payload size, with duplicates removed. Speedup is baseline median latency divided by pooled median latency.